package admin

import (
	"encoding/json"
	"go-cmdb/models"
)

type FormController struct {
	AdminController
}

func (ctrl *FormController) Prepare() {

	//匿名函数配置（不受后端鉴权控制）
	ctrl.AllowAnonymous(func(act string) bool {
		return act == "Preview"
	})
	ctrl.AdminController.Prepare()
	ctrl.IgnoreLayout(func(act string) bool {
		return act == "Preview"
	})

}

func NewFormController() *FormController {
	return &FormController{}
}

func (ctrl *FormController) Index() {

	ctrl.ViewData(map[string]interface{}{
		"title": "表单实例",
	})
	return

}

func (ctrl *FormController) Design() {

	id, _ := ctrl.GetUint64("id", 0)
	f, _ := models.NewForm().GetFormDesign(id)

	ctrl.ViewData(map[string]interface{}{
		"title": "表单设计",
		"id":    id,
		"form":  f.ToJson(),
	})
	return

}
func (ctrl *FormController) Page() {

	var (
		pageSize, _  = ctrl.GetUint64("size", 0)
		pageIndex, _ = ctrl.GetUint64("page", 0)
	)

	lst, count, _ := models.NewForm().GetByPage(pageSize, pageIndex)

	ctrl.OK(map[string]interface{}{
		"rows":  lst,
		"total": count,
	})
	return

}
func (ctrl *FormController) SaveDesign() {

	var req models.SaveFormDesignReq

	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	uid := ctrl.GetUserID()
	if err := models.NewForm().SaveDesign(uid, req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	ctrl.OK(nil)
	return

}
func (ctrl *FormController) Add() {

	var req models.AddFormReq

	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	uid := ctrl.GetUserID()
	if err := models.NewForm().Add(uid, req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	ctrl.OK(nil)
	return

}
func (ctrl *FormController) Find() {

	id, _ := ctrl.GetUint64("id", 0)
	m, _ := models.NewForm().Find(id)
	ctrl.OK(m)
	return

}

func (ctrl *FormController) Update() {

	var req models.UpdateFormReq

	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	uid := ctrl.GetUserID()
	if err := models.NewForm().Update(uid, req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	ctrl.OK(nil)
	return

}
func (ctrl *FormController) Remove() {

	var req models.RemoveFormReq

	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	uid := ctrl.GetUserID()
	if err := models.NewForm().CStatus(0, req.ID, uid); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	ctrl.OK(nil)
	return

}
func (ctrl *FormController) Enable() {

	var req models.EnableFormReq

	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	uid := ctrl.GetUserID()
	if err := models.NewForm().CStatus(req.Status, req.ID, uid); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	ctrl.OK(nil)
	return

}

func (ctrl *FormController) Preview() {

	uuid := ctrl.GetString(":uuid")
	f, _ := models.NewForm().GetFormDesignByUUID(uuid)

	ctrl.ViewData(map[string]interface{}{
		"id":        f.FormID,
		"form":      f.ToJson(),
		"form_name": f.FormName,
	})
	return

}
